package com.fosun.spr.plugin.uuc.api.uucUser.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.uuc.api.uucUser.dao.UucUserDao;
import com.fosun.spr.plugin.uuc.api.uucUser.service.UucUserService;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.DeleteSpModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.DeleteSrModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.GetSpModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.GetSrModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.ListSpModel;
import com.fosun.spr.plugin.uuc.api.uucUser.service.model.ListSrModel;
import com.fosun.spr.plugin.uuc.helper.mybatis.dao.SprUucUserMapper;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUser;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUser;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUserExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class UucUserServiceImpl extends CoBaseService implements
		UucUserService {
	@Autowired 
	private UucUserDao uucUserDao;
	@Autowired
	private SprUucUserMapper sprUucUserMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(uucUserDao.list(spModel));
		srModel.setTotalCount(uucUserDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		SprUucUser db = sprUucUserMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"帐号管理");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprUucUser db = sprUucUserMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		sprUucUserMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getLoginNo(),"帐号名");
		ExtAsserts.notNull(spModel.getUserName(),"姓名");
		ExtAsserts.notNull(spModel.getMiPsd(),"密码");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		SprUucUserExample eLoginNo = new SprUucUserExample();
		SprUucUserExample.Criteria cLoginNo = eLoginNo.createCriteria();
		cLoginNo.andLoginNoEqualTo(spModel.getLoginNo());
		cLoginNo.andIsDeletedEqualTo(IS_DELETED_N);
		List<SprUucUser> rowsLoginNo = sprUucUserMapper.selectByExample(eLoginNo);		
		
		if(StringUtils.isBlank(spModel.getId())){
			if(rowsLoginNo.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getLoginNo()+" 已经存在");
			}		
			
			SprUucUser db = new SprUucUser();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			sprUucUserMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsLoginNo.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getLoginNo()+" 已经存在");
			}			
			if(rowsLoginNo.size()==1 && !rowsLoginNo.get(0).getLoginNo().equals(spModel.getLoginNo())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getLoginNo()+" 已经存在");
			}	
			
			SprUucUser db = sprUucUserMapper.selectByPrimaryKey(spModel.getId());
			db.setLoginNo(spModel.getLoginNo());
			db.setUserName(spModel.getUserName());
			db.setMobile(spModel.getMobile());
			db.setEmail(spModel.getEmail());
			db.setMiPsd(spModel.getMiPsd());
			db.setMemo(spModel.getMemo());
			sprUucUserMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
